package com.CMS.client.datasource;

import java.util.List;

import com.CMS.client.MenuService;
import com.CMS.client.MenuServiceAsync;
import com.CMS.server.domain.Menu;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class MenuDataSource extends BaseDataSource{

	@Override
	protected void executeFetch(final String requestId, final DSRequest request,
			final DSResponse response) {
		
		MenuServiceAsync rpc = GWT.create(MenuService.class);
		rpc.fetch(new AsyncCallback<List<Menu>>() {
			
			@Override
			public void onSuccess(List<Menu> result) {
				final int startIndex = (request.getStartRow() < 0) ? 0 : request.getStartRow();
				final int endIndex = (request.getEndRow ()== null)? -1 : request.getEndRow();
				int size = result.size ();
                if (endIndex >= 0) {
                	if (endIndex < startIndex) {
                		size = 0;
                    }
                    else {
                        size = endIndex - startIndex + 1;
                    }
                }
                //Creamos los registros a devolver
				Record[] regs = new Record[result.size()];
				if (size > 0) {
					for (int i = 0; i < regs.length; i++) {
						Record record = new Record();
						copyValues(result.get(i), record);
						regs[i] = record;
					}
				}
				response.setData(regs);
				//IMPORTANTE: para que funcione el paginado debemos especificar
				//el total de registro del result set
				response.setTotalRows (result.size ());
				processResponse(requestId, response);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				response.setStatus(RPCResponse.STATUS_FAILURE);
				processResponse(requestId, response);
			}
		});
	}

	@Override
	protected void executeAdd(final String requestId, DSRequest request,
			final DSResponse response) {
		
		MenuServiceAsync rpc = GWT.create(MenuService.class);	
		final ListGridRecord rec = new ListGridRecord (request.getData());
		rpc.create(rec.getAttribute(getDescription()), new AsyncCallback<Void>() {			
			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				response.setStatus(RPCResponse.STATUS_FAILURE);
				processResponse(requestId, response);
			}
		});
	}

	@Override
	protected void executeUpdate(final String requestId, DSRequest request,
			final DSResponse response) {
		
		MenuServiceAsync rpc = GWT.create(MenuService.class);
		Record old = request.getOldValues();
		final ListGridRecord rec = new ListGridRecord (request.getData());
		rpc.update((rec.getAttributeAsInt(getId()) == null) ? old.getAttributeAsInt(getId()) : rec.getAttributeAsInt(getId()),
				   (rec.getAttributeAsBoolean(getFlagDelete()) == null) ? old.getAttributeAsBoolean(getFlagDelete()) : rec.getAttributeAsBoolean(getFlagDelete()),
				   (rec.getAttribute(this.getDescription()) == null) ? old.getAttribute(getDescription()) : rec.getAttribute(this.getDescription()),
					new AsyncCallback<Menu>() {
						@Override
						public void onFailure(Throwable caught) {
							response.setStatus(RPCResponse.STATUS_FAILURE);
							processResponse(requestId, response);
						}
						@Override
						public void onSuccess(Menu result) {
							// TODO Auto-generated method stub
						}						
				});
	}

	@Override
	protected void executeRemove(final String requestId, DSRequest request,
			final DSResponse response) {

		MenuServiceAsync rpc = GWT.create(MenuService.class);
		final ListGridRecord rec = new ListGridRecord (request.getData());
		rpc.delete(rec.getAttributeAsInt(getId()), new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				response.setStatus(RPCResponse.STATUS_FAILURE);
				processResponse(requestId, response);
			}
			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
			}
		});
	}
	
	private void copyValues(Menu from, Record to) {
		to.setAttribute(getDescription(), from.getDescription());
		to.setAttribute(getId(), from.getId());
		to.setAttribute(getAddDate(), from.getAddDate());
		to.setAttribute(getModDate(), from.getModDate());
		to.setAttribute(getFlagDelete(), from.getFlagDelete());
	}

}
